package com.sabm.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sabm.model.Address;
import com.sabm.model.Contact;

@Repository
public class AddressDAOImpl implements AddressDAO{
	@Autowired
    private SessionFactory sessionFactory;
	//
	public void save(Address address){
		sessionFactory.getCurrentSession().save(address);
	}
	//
	public void delete(Address address){
		sessionFactory.getCurrentSession().delete(address);
	}
	//
	public void update(Address address){
		sessionFactory.getCurrentSession().update(address);
	}
	//
	public void saveOrUpdate(Address address){
		sessionFactory.getCurrentSession().saveOrUpdate(address);
	}
	//
	public Address getAddressById(int idAddress){
		Query query = sessionFactory.getCurrentSession().createQuery("from Address where idAddress = :idAddress ");
		query.setParameter("idAddress", idAddress);
		List<Address> list = query.list();
		if(list.size() > 0){
			return list.get(0);
		}else{
			return null;
		}
	}

}
